package com.tygz.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.auto.base.mapper.SysModuleMapper;
import com.auto.base.mapper.SysParameterMapper;
import com.auto.base.mapper.SysRightsMapper;
import com.auto.base.pojo.SysModule;
import com.auto.base.pojo.SysModuleExample;
import com.auto.base.pojo.SysParameter;
import com.auto.base.pojo.SysParameterExample;
import com.auto.base.pojo.SysRightsExample;
import com.auto.util.SystemUtil;
import com.base.common.CommonConstants;
import com.base.util.JUtil;
import com.tygz.service.SysRefreshService;


@Transactional
@Service
@Scope("prototype")
public class SysRefreshServiceImpl implements SysRefreshService {

	@Resource
	private SysParameterMapper  sysParameterMapper;
	
	
	@Resource
	private SysModuleMapper sysModuleMapper;
	
	@Resource
	private SysRightsMapper sysRightsMapper;
	
	//刷新访问路径
	@Override
	public void refreshSysAllowUrl(String pks) throws Exception{
		
		if (JUtil.isNull(pks)) {
            throw new RuntimeException("删除主键不能为空");
        }
		SysModuleExample example = new SysModuleExample();
        // 设置查询条件
		SysModuleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(JUtil.splitToListInteger(pks));
        // 执行操作
        int res = sysModuleMapper.deleteByExample(example);
        // 抛出异常
        if (res < 0){
        throw new RuntimeException("SysModule表删除数据失败");
        }
        
        SysRightsExample example1 = new SysRightsExample();
        // 设置查询条件
        SysRightsExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andModuleIdIn(JUtil.splitToListInteger(pks));
        // 执行操作
        sysRightsMapper.deleteByExample(example1);
        
        example = null;
        example1 = null;
        criteria = null;
        criteria1 = null;
        refreshSysAllowUrl();
	}

	//刷新系统参数
	@Override
	public void refreshSysParam() throws Exception{
			Map<String,String> paramMap = new HashMap<String, String>(); 
			SysParameterExample example = new SysParameterExample();
			SysParameterExample.Criteria criteria = example.createCriteria();
			criteria.andEnabledEqualTo(CommonConstants.ENABLE);
			List<SysParameter> paramsList = sysParameterMapper.selectByExample(example);
			if(null != paramsList && !paramsList.isEmpty()){
				for(SysParameter c : paramsList){
					String mapKey = c.getParameterKey();
					paramMap.put(mapKey, c.getParameterValue());
				}
			}
			SystemUtil.getInstance().setParamMap(paramMap);
	}

	@Override
	public void refreshSysAllowUrl() throws Exception {
		// TODO Auto-generated method stub
		SysModuleExample example = new SysModuleExample();
        // 设置查询条件
		SysModuleExample.Criteria criteria = example.createCriteria();
		Set<String> allowUrl = new HashSet<String>(); 
		criteria.andEnabledEqualTo(CommonConstants.ENABLE);
		List<SysModule> paramsList = sysModuleMapper.selectByExample(example);
		if(null != paramsList && !paramsList.isEmpty()){
				for(SysModule c : paramsList){
					if(!JUtil.isNull(c.getModuleUrl()) && !"/".equals(c.getModuleUrl())){
						String url  = c.getModuleUrl();
						 url = url.replace("/", "");
						allowUrl.add(url);
					}
				}
			}
			
			SystemUtil.getInstance().setAllowUrl(allowUrl);
	}

}
